<?php

class AdminController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
		$this->_helper->layout()->setLayout('admin');
    }

    public function indexAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];


		$formEvento = new Application_Form_Evento();
		$formEvento -> setMethod("post");
		$formEvento -> setAction("/admin/evento");
		$this -> view -> formEvento = $formEvento;		

        $formNoticia = new Application_Form_Noticia();
		$formNoticia -> setMethod("post");
		$formNoticia -> setAction("/admin/noticia");
		$this -> view -> formNoticia = $formNoticia;		
    }
	
	public function loginAction()
    {
        // action body
       if(Zend_Session::namespaceIsset('alc')){
            $session = Zend_Session::namespaceGet('alc');
            $this->_redirect('/admin/index');
       }else{
            $admin = new Application_Model_Admin();
            $formLogin = new Application_Form_Login();
            $formLogin -> setMethod("post");
            $formLogin -> setAction("/admin/login");

            $this -> view -> formLogin = $formLogin;

            if($this->getRequest()->isPost()){

                if($formLogin->isValid($_POST)){

                    $data = $formLogin->getValues();

                    $loginSuccess = $admin->verifyLogin($data["name"], $data["password"]);
                    $loginSuccess = $loginSuccess->toArray();
                   if($loginSuccess){
                       $userName = $loginSuccess["nombre"];
                       $userID = $loginSuccess["id"];
                       $ns = new Zend_Session_Namespace('alc');
                       $ns->userName = $userName;
                       $ns->userID = $userID;

                       $this->_redirect('/admin/index');

                   }else{
                       $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Su usuario y password no son validas. Intente de nuevo.</div>";
                   }
               }
           }
       }
    }

    public function logoutAction()
    {
        Zend_Session::namespaceUnset("alc");
        $this->_redirect("/admin/login");
    }
	
/*::::::::::::::::::::::::::::::::::   CREATE FUNCTIONS   ::::::::::::::::::::::::::::::::::*/

    public function noticiaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
					
            $formNoticia = new Application_Form_Noticia();
            $formNoticia -> setMethod("post");
            $formNoticia -> setAction("/admin/noticia");

            $this -> view -> formNoticia = $formNoticia;

            if($this->getRequest()->isPost()){
                if($formNoticia->isValid($_POST)){
                    $obj_noticias = new Application_Model_Noticia();

                    if($_FILES["foto"]){
                        $foto = $obj_noticias->uploadFoto($_FILES["foto"], "noticias");
                    }else{
                        $foto = null;
                    }

                    if($_FILES["foto_slider"]){
                        $fotoSlider = $obj_noticias->uploadFoto($_FILES["foto_slider"], "slider");
                    }else{
                        $fotoSlider = null;
                    }

                    $title = $_POST["titulo"];
                    $descripcion = $_POST["descripcion"];
                    $texto = $_POST["texto"];
                    $galeria_id = isset($_POST["galeria_id"]) ?  $_POST["galeria_id"] : null;
                    $video_id = isset($_POST["video_id"]) ?  $_POST["video_id"] : null;
                    $fecha = date("Y-m-d");
                    $status = 'A';

                    $id = $obj_noticias->addNoticia($title, $foto, $galeria_id, $video_id, $fotoSlider, $descripcion, $texto, $fecha, $status);

                    if($id){
                        $this->_redirect("/admin/noticias");
                    }else{
                        $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                    }
                }
            }
        }
    }

    public function eventoAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $session = Zend_Session::namespaceGet('alc');
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
					
            $formEvento = new Application_Form_Evento();
            $formEvento -> setMethod("post");
            $formEvento->removeElement("id");
            $formEvento -> setAction("/admin/evento");

            $evento = new Application_Model_Evento();

            $this -> view -> formEvento = $formEvento;

            if($this->getRequest()->isPost()){

                if($formEvento->isValid($_POST)){

                    $data = $formEvento->getValues();

                    $titulo = $data["titulo"];
                    $hora = date("H:i", strtotime($data["fecha"]));
                    $fecha = date("Y-m-d", strtotime($data["fecha"]));
                    $lugar = $data["lugar"];
                    $descripcion = $data["descripcion"];
                    $principal = isset($data["principal"]) ? $data["principal"] : 0;

                    $id = $evento->addEvento($titulo, $hora, $fecha, $lugar, $descripcion, $principal, $id);

                    if($id){
                        $this->_redirect("/admin/eventos");
                    }else{
                        $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                    }
                }
            }
        }
    }

    public function galeriaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
		
            $formGaleria = new Application_Form_Galeria();
            $formGaleria -> setMethod("post");
            $formGaleria -> setAction("/admin/galeria");
            $this -> view -> formGaleria = $formGaleria;

            if($this->getRequest()->isPost()){
                $titulo = $_POST["titulo"];
                $fecha = date("Y-m-d");
                $status = "A";

                $obj_galeria = new Application_Model_Galeria();
                $id = $obj_galeria->addGaleria($titulo, $fecha, $status);

                if($id){
                    $this->_redirect("/admin/galerias");
                }else{
                    $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                }

            }
        }
    }

    public function fotoAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $request = $this->getRequest();
        $galeria_id =$request->getParam("id");


		$formFoto = new Application_Form_Foto();
		$formFoto -> setMethod("post");
		$formFoto -> setAction("/admin/foto/id/".$galeria_id);

        $this->view->galeria_id = $galeria_id;
        $this -> view -> formFoto = $formFoto;

        if($this->getRequest()->isPost()){

            $obj_foto = new Application_Model_Foto();

            for($i=1; $i<6; $i++){
                if($_FILES["image".$i]["name"] != ""){
                    $nombre = $obj_foto->uploadFoto($_FILES["image".$i]);
                    $description = $_POST["description".$i];
                    $obj_foto->addFoto($nombre, $galeria_id, $description, "A");
                }
            }
        }
    }

    public function videoAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
		
            $formVideo = new Application_Form_Video();
            $formVideo -> setMethod("post");
            $formVideo -> setAction("/admin/video");

            $this -> view -> formVideo = $formVideo;

            if($this->getRequest()->isPost()){
                $titulo = $_POST["titulo"];
                $url = $_POST["url"];
                $fecha = date("Y-m-d");
                $status = "A";

                $obj_video = new Application_Model_Video();
                $id = $obj_video->addVideo($titulo, $url, $fecha, $status);

                if($id){
                    $this->_redirect("/admin/videos");
                }else{
                    $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                }
            }
        }
    }
	
    public function revistaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $formRevista = new Application_Form_Revista();
        $formRevista -> setMethod("post");
        $formRevista -> setAction("/admin/revista");
        $this -> view -> formRevista = $formRevista;

        if($this->getRequest()->isPost()){

            $obj_revista = new Application_Model_Revista();

            /*for($i=1; $i<6; $i++){
                if($_FILES["image".$i]["name"] != ""){
                    $nombre = $obj_foto->uploadFoto($_FILES["image".$i]);
                    $description = $_POST["description".$i];
                    $obj_foto->addFoto($nombre, $galeria_id, $description, "A");
                }
            }
            echo "<pre>"; print_r($_FILES); exit();
            */
        }
    }

    public function cartonAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $formCarton = new Application_Form_Carton();
        $formCarton -> setMethod("post");
        $formCarton -> setAction("/admin/carton");
		$this -> view -> formCarton = $formCarton;

        if($this->getRequest()->isPost()){

            $obj_carton = new Application_Model_Carton();

            if($_FILES["image"]["name"] != ""){
                $imagen = $obj_carton->uploadFoto($_FILES["image"]);
            }else{
                $imagen = "";
            }

            $titulo = $_POST["titulo"];
            $fecha = date("Y-m-d");
            $status = "A";

            $id = $obj_carton->addCarton($titulo, $fecha, $imagen, $status);

            if($id){
                $this->_redirect("/admin/cartones");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
            }
        }
    }	
	
/*::::::::::::::::::::::::::::::::::   LIST FUNCTIONS   ::::::::::::::::::::::::::::::::::*/
	
    public function noticiasAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_noticia = new Application_Model_Noticia();
        $noticias = $obj_noticia->getAll();

        $this->view->noticias = $noticias;

    }
	
    public function eventosAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];
        
		$obj_eventos = new Application_Model_Evento();
        $eventos = $obj_eventos->getEventos();

        $this->view->eventos = $eventos;
    }	

    public function galeriasAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_galerias = new Application_Model_Galeria();
        $galerias = $obj_galerias->getGalerias();

        $this->view->galerias = $galerias;
    }

    public function fotosAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $request = $this->getRequest();
        $galeria_id =$request->getParam("id");

        $obj_galerias = new Application_Model_Galeria();
        $galeria = $obj_galerias->getById($galeria_id);

        $this->view->galeria = $galeria;
    }

    public function videosAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }
		
        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_videos = new Application_Model_Video();
        $videos = $obj_videos->getLast20();

        $this->view->videos = $videos;
    }

    public function revistasAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_revistas = new Application_Model_Revista();
        $revistas = $obj_revistas->getLast20();

        $this->view->revistas = $revistas;

    }
	
    public function cartonesAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_cartones = new Application_Model_Carton();
        $cartones = $obj_cartones->getLast20();

        $this->view->cartones = $cartones;
    }

    public function contactosAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $obj_contactos = new Application_Model_Contacto();
        $contactos = $obj_contactos->getAll();

        $this->view->contactos = $contactos;
    }
	
/*::::::::::::::::::::::::::::::::::   EDIT FUNCTIONS   ::::::::::::::::::::::::::::::::::*/

	public function editnoticiaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];

            $request = $this->getRequest();
            $noticia_id = $request->getParam("id");

            $formNoticia = new Application_Form_Noticia();
            $formNoticia -> setMethod("post");
            $formNoticia -> setAction("/admin/editnoticia/id/".$noticia_id);

            $obj_noticias = new Application_Model_Noticia();

            if($noticia_id){
                $noticia = $obj_noticias->find($noticia_id)->current();
                $arr_noticia = $noticia->toArray();
                $formNoticia->populate($arr_noticia);
            }

            $this -> view -> formNoticia = $formNoticia;

            if($this->getRequest()->isPost()){
                if($formNoticia->isValid($_POST)){

                    if($_FILES["foto"]["name"] != ""){
                        $foto = $obj_noticias->uploadFoto($_FILES["foto"], "noticias");
                    }else{
                        $foto = null;
                    }

                    if($_FILES["foto_slider"]["name"] != ""){
                        $fotoSlider = $obj_noticias->uploadFoto($_FILES["foto_slider"], "slider");
                    }else{
                        $fotoSlider = null;
                    }

                    $id = $_POST["id"];
                    $title = $_POST["titulo"];
                    $descripcion = $_POST["descripcion"];
                    $texto = $_POST["texto"];
                    $galeria_id = isset($_POST["galeria_id"]) ?  $_POST["galeria_id"] : null;
                    $video_id = isset($_POST["video_id"]) ?  $_POST["video_id"] : null;
                    $fecha = date("Y-m-d");
                    $status = 'A';

                    $result = $obj_noticias->updateNoticia($title, $foto, $galeria_id, $video_id, $fotoSlider, $descripcion, $texto, $fecha, $status, $id);

                    if($result){
                        $this->_redirect("/admin/noticias");
                    }else{
                        $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                    }
                }
            }
        }
    }
	
	public function editeventoAction()
	{
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $session = Zend_Session::namespaceGet('alc');
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
					
            $request = $this->getRequest();
            $evento_id = $request->getParam("id");

            $formEvento = new Application_Form_Evento();
            $formEvento -> setMethod("post");
            $formEvento -> setAction("/admin/editevento/id/".$evento_id);

            $evento = new Application_Model_Evento();

            if($evento_id){
                $evento_obj = $evento->find($evento_id)->current();
                $arr_evento = $evento_obj->toArray();
                $arr_evento["fecha"] = $arr_evento["fecha"]." ".$arr_evento["hora"];
                $formEvento->populate($arr_evento);
            }

            $this -> view -> formEvento = $formEvento;

            if($this->getRequest()->isPost()){
                if($formEvento->isValid($_POST)){
                    $data = $formEvento->getValues();

                    $id = $data["id"];
                    $titulo = $data["titulo"];
                    $hora = date("H:i", strtotime($data["fecha"]));
                    $fecha = date("Y-m-d", strtotime($data["fecha"]));
                    $lugar = $data["lugar"];
                    $descripcion = $data["descripcion"];
                    $principal = isset($data["principal"]) ? $data["principal"] : 0;

                    $result = $evento->updateEvento($titulo, $hora, $fecha, $lugar, $descripcion, $principal, $id);

                    if($result){
                        $this->_redirect("/admin/eventos");
                    }else{
                        $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                    }
                }
            }
        }
    }	
	
	public function editgaleriaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];
		
            $request = $this->getRequest();
            $galeria_id = $request->getParam("id");

            $formGaleria = new Application_Form_Galeria();
            $formGaleria -> setMethod("post");
            $formGaleria -> setAction("/admin/editgaleria/id/".$galeria_id);

            $obj_galeria = new Application_Model_Galeria();

            if($galeria_id){
                $galeria = $obj_galeria->find($galeria_id)->current();
                $arr_galeria = $galeria->toArray();
                $formGaleria->populate($arr_galeria);
            }

            $this -> view -> formGaleria = $formGaleria;

            if($this->getRequest()->isPost()){
                $titulo = $_POST["titulo"];

                $obj_galeria = new Application_Model_Galeria();
                $result = $obj_galeria->updateGaleria($titulo, $galeria_id);

                if($result){
                    $this->_redirect("/admin/galerias");
                }else{
                    $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
                }

            }
        }
    }

    public function editcartonAction(){
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }

        $session = Zend_Session::namespaceGet('alc');
        $this->view->userName = $session["userName"];

        $request = $this->getRequest();
        $carton_id = $request->getParam("id");

        $obj_carton = new Application_Model_Carton();

        $carton = $obj_carton->find($carton_id)->current()->toArray();

        $formCarton = new Application_Form_Carton();
        $formCarton -> setMethod("post");
        $formCarton -> setAction("/admin/editcarton/id/".$carton_id);

        $formCarton->populate($carton);

        $this -> view -> formCarton = $formCarton;

        if($this->getRequest()->isPost()){

            if($_FILES["imagen"]["name"] != ""){
                $imagen = $obj_carton->uploadFoto($_FILES["imagen"]);
            }else{
                $imagen = "";
            }

            $titulo = $_POST["titulo"];

            $result = $obj_carton->updateCarton($titulo, $imagen, $carton_id);

            if($result){
                $this->_redirect("/admin/cartones");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> Hubo un error al guardar los datos. Intente de nuevo.</div>";
            }
        }
    }

/*::::::::::::::::::::::::::::::::::   DELETE FUNCTIONS   ::::::::::::::::::::::::::::::::::*/
	
    public function deletenoticiaAction(){
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $session = Zend_Session::namespaceGet('alc');
            $this->_redirect('/admin/login');
        }else{
		
            $noticia = new Application_Model_Noticia();
            $request = $this->getRequest();
            $noticia_id = $request->getParam("id");

            $row = $noticia->find($noticia_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/noticias");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar esta Noticia. Intente de nuevo.</div>";
            }
        }
    }	

    public function deleteeventoAction(){
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $session = Zend_Session::namespaceGet('alc');
            $this->_redirect('/admin/login');
        }else{
		
            $evento = new Application_Model_Evento();
            $request = $this->getRequest();
            $evento_id = $request->getParam("id");

            $row = $evento->find($evento_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/eventos");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar este Evento. Intente de nuevo.</div>";
            }
        }
    }

    public function deletegaleriaAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
            $request = $this->getRequest();
            $galeria_id = $request->getParam("id");


            $obj_galeria = new Application_Model_Galeria();

            $row = $obj_galeria->find($galeria_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/galerias");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar esta Galeria. Intente de nuevo.</div>";
            }
        }
    }
	
    public function deletefotoAction()
    {
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
            $request = $this->getRequest();
            $foto_id = $request->getParam("id");
            $galeria_id = $request->getParam("galeria_id");


            $obj_foto = new Application_Model_Foto();

            $row = $obj_foto->find($foto_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/fotos/id/".$galeria_id);
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar esta Foto. Intente de nuevo.</div>";
            }
        }

    }

    public function deletevideoAction(){
        // action body
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $session = Zend_Session::namespaceGet('alc');
            $this->view->userName = $session["userName"];

            $obj_video = new Application_Model_Video();
            $request = $this->getRequest();
            $video_id = $request->getParam("id");

            $row = $obj_video->find($video_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/videos");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar este Video. Intente de nuevo.</div>";
            }
        }
    }
	
    public function deleterevistaAction(){
        if(!Zend_Session::namespaceIsset('alc')){
            $this->_redirect('/admin/login');
        }else{
		
            $obj_revistas = new Application_Model_Revista();
            $request = $this->getRequest();
            $revista_id = $request->getParam("id");

            $row = $obj_revistas->find($revista_id)->current();
            $row->status = 'S';
            if($row->save()){
                $this->_redirect("/admin/revistas");
            }else{
                $this->view->mensajeError = "<div class='error'><strong>Error:</strong> No se pudo borrar esta Revista. Intente de nuevo.</div>";
            }
        }
    }	
   
}